package com.lenovo.newsapp.activity;

import java.io.File;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;
import cn.jpush.android.api.JPushInterface;

import com.adobe.mobile.Analytics;
import com.adobe.mobile.Config;
import com.lenovo.newsapp.R;
import com.lenovo.newsapp.util.Const;
import com.lenovo.newsapp.util.FileUtil;
import com.lenovo.newsapp.util.PreferenceUtil;

public class SettingActivity extends BaseActivity implements OnClickListener {

	private TextView backBtnTV;
	private TextView settingBtnTV;
	private TextView titleTV;
	private Button button;

	private Spinner mSpinner;
	private TextView version;
	private Switch pushnewsSW;
//	private SwitchPush pushnewsSW;
	private Switch offLineSW;
	private Switch languangeSW;
	private Button logoutBtn;
	private RelativeLayout settingReadingPreferencesRL;
	private TextView readPreferencesLableTV;
	private TextView languageLableTV;
	private TextView settingLocalCacheTV;
	private TextView offLineLableTV;
	private TextView pushNewsLableTV;
	private TextView versionTV;
	private Button downloadBtn;
	
	private String strversion;
	private final Handler handler = new Handler();
	private Bitmap bitmap;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);
		actionBar.setTitle(R.string.action_settings);
		Log.v(Const.DEBUG, "createisChangeLanguage--" + Const.isChangeLanguage);
		// titleTV = (TextView) findViewById(R.id.red_bar_title_tv);
		// titleTV.setText(R.string.setting_name);
		// settingBtnTV = (TextView) findViewById(R.id.red_bar_setting_btn_tv);
		// settingBtnTV.setVisibility(8);

		// backBtnTV = (TextView) findViewById(R.id.red_bar_back_btn_tv);
		// backBtnTV.setVisibility(0);
		// backBtnTV.setOnClickListener(this);

		// 缓存
		button = (Button) findViewById(R.id.clean);
		
		button.setOnClickListener(this);

		// version
		version = (TextView) findViewById(R.id.version);
		strversion = getVersion();
		version = (TextView) findViewById(R.id.version);
		version.setText(strversion);
		
		readPreferencesLableTV = (TextView) findViewById(R.id.read_preferences_lable_tv);
		languageLableTV = (TextView) findViewById(R.id.language_lable_tv);
		settingLocalCacheTV = (TextView) findViewById(R.id.textView2);
		offLineLableTV = (TextView) findViewById(R.id.off_line_lable_tv);
		pushNewsLableTV = (TextView) findViewById(R.id.push_news_lable_tv);
		versionTV = (TextView) findViewById(R.id.textView1);

		// 退出登录
		logoutBtn = (Button) findViewById(R.id.logout_btn);
		logoutBtn.setOnClickListener(this);
		
		downloadBtn = (Button) findViewById(R.id.download_btn);
		downloadBtn.setOnClickListener(this);
		
		settingReadingPreferencesRL = (RelativeLayout) findViewById(R.id.setting_reading_preferences_rl);
		settingReadingPreferencesRL.setOnClickListener(this);

		// Push news
		pushnewsSW = (Switch) findViewById(R.id.pushnewsSW);

		Log.v("ddddd", PreferenceUtil.getString("push", "open"));
		// 判断起始状态
		if (PreferenceUtil.getString("push", "open").equals("")) {
			pushnewsSW.setChecked(true);
			PreferenceUtil.commitString("push", "open");
		} else if (PreferenceUtil.getString("push", "open").equals("open")) {
			Log.v("ddddd", "开");
			pushnewsSW.setChecked(true);
			// PreferenceUtil.commitString("push", "open");
		} else if (PreferenceUtil.getString("push", "open").equals("off")) {
			Log.v("ddddd", "关");
			pushnewsSW.setChecked(false);
			// PreferenceUtil.commitString("push", "off");
		}
		// 监听
		pushnewsSW.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@SuppressLint("NewApi")
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				Log.v("dddd", isChecked+"---");
				if (isChecked == true) {
				PreferenceUtil.commitString("push", "open");
				JPushInterface.resumePush(getApplicationContext());
			} else {
				PreferenceUtil.commitString("push", "off");
				JPushInterface.stopPush(getApplicationContext());
			}
			}
		});

		// OfflineMode

//		offLineSW = (Switch) findViewById(R.id.offLineSW);
//		// 判断起始状态
//		if (PreferenceUtil.getString("offline", "open").equals("")) {
//			offLineSW.setChecked(true);
//			PreferenceUtil.commitString("offline", "open");
//		} else if (PreferenceUtil.getString("offline", "open").equals("open")) {
//			offLineSW.setChecked(true);
//			// PreferenceUtil.commitString("offline", "open");
//		} else if (PreferenceUtil.getString("offline", "open").equals("off")) {
//			offLineSW.setChecked(false);
//			// PreferenceUtil.commitString("offline", "off");
//		}
//		//离线 监听
//		offLineSW.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//			@SuppressLint("NewApi")
//			@Override
//			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//				if (isChecked == true) {
//
//					PreferenceUtil.commitString(Const.OFFLINE, "open");
//				} else {
//					PreferenceUtil.commitString(Const.OFFLINE, "off");
//				}
//			}
//		});

		languangeSW = (Switch) findViewById(R.id.languangeSW);
		if (PreferenceUtil.getString("language", "en").equals("")) {
			languangeSW.setChecked(true);
			PreferenceUtil.commitString("language", "en");
		} else if (PreferenceUtil.getString("language", "en").equals("en")) {
			Log.v("ddddd", "开");
			languangeSW.setChecked(true);
			// PreferenceUtil.commitString("push", "open");
		} else if (PreferenceUtil.getString("language", "en").equals("ch")) {
			Log.v("ddddd", "关");
			languangeSW.setChecked(false);
			// PreferenceUtil.commitString("push", "off");
		}
		Log.v("dd", PreferenceUtil.getString("language", ""));
		languangeSW.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@SuppressLint("NewApi")
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				Log.v("dddd", isChecked+"---");
				Const.isChangeLanguage = true;
				if (isChecked == true) {
					switchLanguage("en");
				} else {
					switchLanguage("ch");
				}
				actionBar.setTitle(R.string.action_settings);
				pushNewsLableTV.setText(R.string.setting_pushnews);
				offLineLableTV.setText(R.string.setting_offlinemode);
				settingLocalCacheTV.setText(R.string.setting_local_cache);
				button.setText(R.string.setting_clean);
				languageLableTV.setText(R.string.setting_language);
				readPreferencesLableTV.setText(R.string.setting_readingpreferences);
				versionTV.setText(R.string.setting_version);
				logoutBtn.setText(R.string.setting_logout);
				downloadBtn.setText(R.string.setting_download);
			}
		});
		
		Analytics.trackState("SettingActivity", null);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		Config.pauseCollectingLifecycleData();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		Config.collectLifecycleData();
	}

	// 获取版本号
	public String getVersion() {
		try {
			PackageManager manager = this.getPackageManager();
			PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
			String version = info.versionName;
			return version;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v.getId() == R.id.red_bar_back_btn_tv) {
			finish();
		} else if (v.getId() == R.id.clean) {
//			Toast.makeText(SettingActivity.this, R.string.not_been_enabled, Toast.LENGTH_LONG).show();
			new AlertDialog.Builder(this).setTitle(R.string.prompt).setMessage(R.string.determine_clear).setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int whichButton) {
					setClean();
					
				}
			}).setNegativeButton(R.string.cancel, null).show();
			
		} else if (v.getId() == R.id.logout_btn) {
			new AlertDialog.Builder(this).setTitle(R.string.prompt).setMessage(R.string.determine_logout).setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int whichButton) {
					Intent it = new Intent(SettingActivity.this, LoginActivity.class);
					startActivity(it);
					PreferenceUtil.commitString("login", "false");
					finishLogoutActivity();
				}
			}).setNegativeButton(R.string.cancel, null).show();
		}else if(v.getId() == R.id.setting_reading_preferences_rl){
			Intent intent = new Intent(SettingActivity.this, CategoryActivity.class);
			startActivity(intent);
//			Toast.makeText(SettingActivity.this, R.string.not_been_enabled, Toast.LENGTH_LONG).show();
		}else if(v.getId() == R.id.download_btn){
			
		}
	}

	private void setClean() {
		Log.v(Const.DEBUG, "清空缓存--");
		showLoadProgressBar();
		PreferenceUtil.commitString(Const.SAVENEWS + "0", "");
		PreferenceUtil.commitString(Const.SAVENEWS + "1", "");
		PreferenceUtil.commitString(Const.SAVENEWS + "2", "");
		PreferenceUtil.commitString(Const.SAVENEWS + "3", "");
		PreferenceUtil.commitString(Const.SAVENEWS + "5", "");
		PreferenceUtil.commitString(Const.SAVENEWS + "7", "");
		String newsDetailId[] = PreferenceUtil.getString("localNewsDetailId", "").split(",");
		Log.v("local news detail id", "changdu = "+newsDetailId.length);
		for(int i=0;i<newsDetailId.length;i++){
			Log.v("local news detail id", newsDetailId[i]);
			PreferenceUtil.removeKey(Const.SAVENEWS + newsDetailId[i]);
		}
		Log.v("local news detail id", "clean changdu = "+newsDetailId.length);
		File file=new File("/storage/emulated/0/Android/data/com.lenovo.newsapp/files");
		FileUtil.delete(file);
		hideLoadProgressBar();
		PreferenceUtil.commitInt("offLineTime", 0);
		showLongToast(R.string.toast_success);
		Log.v(Const.DEBUG, "清空缓存--");
		
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == android.R.id.home) {
			finishWithAnimation();
			return true;
		}

		return super.onOptionsItemSelected(item);
	}
	
}
